<%--
  Created by IntelliJ IDEA.
  User: jiang
  Date: 2021/7/22
  Time: 10:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>租车记录</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/component/pear/css/pear.css"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">车牌号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="num" placeholder="请输入车牌号" class="layui-input"
                               lay-verify="required"/>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">客户姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" lay-verify="title" autocomplete="off" placeholder="请输入客户姓名"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">业务员名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="userName" lay-verify="title" autocomplete="off" placeholder="请输入业务员姓名"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="rent-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="rent-table" lay-filter="rent-table"></table>
    </div>
</div>
</body>
<script type="text/html" id="rent-toolbar">
    <shiro:hasPermission name="rent:delete">
        <button class="pear-btn pear-btn-danger pear-btn-lg" lay-event="clear">
            <i class="layui-icon layui-icon-delete"></i>
            清空
        </button>
    </shiro:hasPermission>
    <shiro:hasPermission name="rent:delete">
        <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
    </shiro:hasPermission>
</script>

<script type="text/html" id="rent-bar">

    <shiro:hasPermission name="rent:delete">
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
                class="layui-icon layui-icon-delete"></i>
        </button>
    </shiro:hasPermission>
    {{#if (d.flag == 1) { }}
    <button class="pear-btn pear-btn-success pear-btn-sm" lay-event="return"><i class="layui-icon layui-icon-return"></i></button>
    {{# } }}

</script>

</script>

<script type="text/html" id="rent-type">
    {{#if (d.type == 1) { }}
    <span>轿车</span>
    {{# }else if(d.type == 2){ }}
    <span>SUV</span>
    {{# }else if(d.type == 3){ }}
    <span>跑车</span>
    {{# } }}
</script>

<script type="text/html" id="rent-flag">
    {{#if (d.flag == 1) { }}
    <span class="layui-bg-red layui-btn-xs layui-btn-radius">未归还</span>
    {{# }else if(d.flag == 2){ }}
    <span class="layui-bg-green layui-btn-xs layui-btn-radius">已归还</span>
    {{# } }}
</script>

<script type="text/html" id="rent-createTime">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}
</script>


<script src="${pageContext.request.contextPath}/resources/component/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/resources/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '车牌号',
                    field: 'num',
                    align: 'center',
                    width: 100
                },
                {
                    title: '车辆类型',
                    field: 'type',
                    align: 'center',
                    templet: '#rent-type'
                },
                {
                    title: '租赁价格',
                    field: 'rentPrice',
                    align: 'center'
                },
                {
                    title: '押金',
                    field: 'deposit',
                    align: 'center',
                    width: 80
                },
                {
                    title: '租赁客户',
                    field: 'name',
                    align: 'center',
                },
                {
                    title: '客户证件',
                    field: 'idCard',
                    align: 'center'
                },
                {
                    title: '租车开始时间',
                    field: 'beginTime',
                    align: 'center',
                    width: 150
                },
                {
                    title: '租车结束时间',
                    field: 'endTime',
                    align: 'center',
                    width: 150
                },
                {
                    title: '出租状态',
                    field: 'flag',
                    align: 'center',
                    templet: '#rent-flag'
                },
                {
                    title: '处理业务员',
                    field: 'userName',
                    align: 'center'
                },
                {
                    title: '创建时间',
                    field: 'createTime',
                    align: 'center',
                    width: 150,
                    templet: '#rent-createTime'
                },
                {
                    title: '操作',
                    toolbar: '#rent-bar',
                    align: 'center',
                    width: 150
                }
            ]
        ]

        table.render({
            elem: '#rent-table',
            url: '${pageContext.request.contextPath}/rent/findByPage.do',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#rent-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
            , parseData: function (res) {
                //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                };
            }
            , response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                , statusCode: 200 //规定成功的状态码，默认：0
                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                , dataName: 'data' //规定数据列表的字段名称，默认：data
            }
        });

        table.on('tool(rent-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'return') {
                window.return(obj);
            }
        });

        table.on('toolbar(rent-table)', function (obj) {
            if (obj.event === 'clear') {
                window.clear();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(rent-query)', function (data) {
            table.reload('rent-table', {
                where: data.field
            })
            return false;
        });

        let mainIndex;

        window.return = function (obj) {
            $.get('${pageContext.request.contextPath}/rent/calcPayMoney.do?id=' + obj.data.id, function(res){
                layer.confirm("确定要归还车辆吗？消费金额为：【 " + res.data + " 】", {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: '${pageContext.request.contextPath}/rent/doReturn.do?id=' + obj.data['id'],
                        type: 'post',
                        success: function (result) {
                            layer.close(loading);
                            if (result.code == 200) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    table.reload('rent-table')
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            });

        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该条记录吗', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: '${pageContext.request.contextPath}/rent/delete.do?id=' + obj.data['id'],
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('')
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.clear = function () {
            layer.confirm('确定要清空所有的记录吗', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: '${pageContext.request.contextPath}/rent/clear.do',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                               table.reload('rent-table')
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {

            var checkIds = common.checkField(obj, 'id');

            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除这些车辆', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: '${pageContext.request.contextPath}/rent/delete.do?id=' + checkIds,
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('rent-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('rent-table');
        }


        form.on('submit(close)', function () {
            table.reload("rent-table");
            layer.close(mainIndex);
            return false;
        });
    })
</script>
</html>

